package com.framework.modules.base.service.impl;

import com.framework.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.PageUtils;
import com.framework.common.utils.Query;

import com.framework.modules.base.dao.UserLocationDao;
import com.framework.modules.base.entity.UserLocationEntity;
import com.framework.modules.base.service.UserLocationService;


@Service("userLocationService")
public class UserLocationServiceImpl extends ServiceImpl<UserLocationDao, UserLocationEntity> implements UserLocationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserLocationEntity> page = this.selectPage(
                new Query<UserLocationEntity>(params).getPage(),
                new EntityWrapper<UserLocationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Object updateUserLocation(UserLocationEntity entity) {
        UserLocationEntity userLocationEntity=baseMapper.selectById(entity.getUid());
        if(null==userLocationEntity){
            entity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
            return this.baseMapper.insert(entity);
        }
        userLocationEntity.setDeviceDetailInfo(entity.getDeviceDetailInfo());
        userLocationEntity.setDeviceType(entity.getDeviceType());
        userLocationEntity.setHistory("lat1:"+userLocationEntity.getLat()+","+"lng1:"+userLocationEntity.getLng());
        userLocationEntity.setIp(entity.getIp());
        userLocationEntity.setLat(entity.getLat());
        userLocationEntity.setLng(entity.getLng());
        return this.baseMapper.updateById(userLocationEntity);
    }

}
